import pytest
import yaml


def pytest_collection_modifyitems(items):
    """
    测试用例收集完成时，将收集到的item的name和nodeid的中文显示
    :return:
    """
    for item in items:
        item.name = item.name.encode("utf-8").decode("unicode_escape")
        item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")


def get_yaml_data():
    """
    读取测试数据
    :return: 返回一个包含测试数据和用例名称的元组
    """
    with open("../datas/testdata.yaml", "r", encoding="utf8") as f:
        yaml_data = yaml.safe_load(f.read())
    test_data = yaml_data.get("datas")
    ids = yaml_data.get("ids")
    return test_data, ids


# 定义fixture 实现数据驱动
@pytest.fixture(params=get_yaml_data()[0], ids=get_yaml_data()[1])
def get_test_data(request):
    return request.param
